﻿using System;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace WebGuiMvc.Helpers.Paging
{
    public static class PagingUrlHelper
    {
        public static string GetPagingUrlTemplate(string rawUrl, NameValueCollection queryStringParameters)
        {
            const string pageParameterTemplate = "page={0}";

            string urlTemplate = null;

            if (!String.IsNullOrEmpty(queryStringParameters["page"]))
            {
                const string pageParameterPattern = @"page=\d*";
                urlTemplate = Regex.Replace(rawUrl, pageParameterPattern, pageParameterTemplate);
            }
            else
            {
                string concatenateSymbol = "?";
                if (queryStringParameters.Count != 0)
                    concatenateSymbol = "&";
                urlTemplate = String.Format("{0}{1}{2}", rawUrl, concatenateSymbol, pageParameterTemplate);
            }

            return urlTemplate;
        }
    }
}